#
# @lc app=leetcode.cn id=1024 lang=python3
#
# [1024] 视频拼接
#

# @lc code=start
from typing import List


class Solution:
    def videoStitching(self, clips: List[List[int]], T: int) -> int:
        clips.sort(key=lambda x: x[0])
        n = right = 0
        while right < T:
            temp = sorted(filter(lambda x: x[0] <= right, clips), key=lambda x: x[1])
            if not temp or temp[-1][1] == right:
                return -1
            right = temp[-1][1]
            n += 1
        return n


# @lc code=end
